package com.fruitstore.jdbc1.controller;

import com.fruitstore.jdbc1.dao.FruitDao;
import com.fruitstore.jdbc1.dao.impl.FruitDaoImpl;
import com.fruitstore.jdbc1.pojo.Fruit;


import java.sql.SQLOutput;
import java.util.List;
import java.util.Scanner;

public class Menu {
    FruitDao fruitDao = new FruitDaoImpl();
    Scanner sc= new Scanner(System.in);

    //显示主菜单
    public int showMainMenu(){
        System.out.println("=================欢迎使用水果库存系统=====================");
        System.out.println("1.查看水果库存列表");
        System.out.println("2.添加水果库存信息");
        System.out.println("3.查看特定水果库存信息");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("======================================================");
        System.out.print("请选择：");

        return sc.nextInt();
    }

    //查看水果库存列表
    public void showFruitList(){
        List<Fruit> fruitList = fruitDao.getFruitList();
        System.out.println("---------------------------------");
        System.out.println("编号\t名称\t单价\t库存\t备注");
        if(fruitList==null || fruitList.size()<0){
            System.out.println("对不起，库存为空");
        }else {
            for (int i = 0; i < fruitList.size(); i++) {
                Fruit fruit = fruitList.get(i);
                System.out.println(fruit);
            }
        }
        System.out.println("---------------------------------");
    }

    //添加水果库存信息  添加及修改：如果水果名不存在则添加，否则更新水果库存数量
    public void addFruit(){
        System.out.println("请输入水果名称： ");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByFname(fname);
        if(fruit==null){
            System.out.println("请输入水果单价：");
            int price = sc.nextInt();
            System.out.println("请输入水果库存量：");
            int fcount = sc.nextInt();
            System.out.println("请输入水果备注：");
            String remark = sc.next();

            // 把用户输入的数据封装到fruit对象
            fruit = new Fruit(0, fname, price, fcount, remark);
            // 调用DAO的添加方法
            fruitDao.addFruit(fruit);
        } else {
            System.out.println("请输入追加的库存量：");
            int fcount = sc.nextInt();
            fruit.setFcount(fruit.getFcount()+fcount);
            //调用DAO的添加方法
            fruitDao.updateFruit(fruit);
        }
        System.out.println("添加成功！");
    }

    //下架水果--删除
    public void delFruit(){
        System.out.println("请输入水果名称：");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByFname(fname);
        if(fruit==null){
            System.out.println("对不起，找不到需要下架的水果信息！");
        }else {
            System.out.println("是否确认下架?（Y/N）");
            String isDel = sc.next();
            if("y".equalsIgnoreCase(isDel)){
                fruitDao.delFruit(fname);
                System.out.println("下架成功！");
            }
        }
    }

    public void showFruitInfo(){
        System.out.println("请输入水果名称：");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByFname(fname);
        if(fruit==null){
            System.out.println("对不起，没有找到指定的水果库存记录！");
        }else {
            System.out.println("--------------------------------");
            System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");
            System.out.println(fruit);
            System.out.println("--------------------------------");
        }
    }

    //退出
    public boolean exit(){
        System.out.println("是否确认退出？（Y/N）");
        String in = sc.next();
        return !"Y".equalsIgnoreCase(in);
    }

}
